﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using PMPUtils.Form;
using PMPUtils.Documentation;
using PMPUtils.Goals;
using PMPUtils.Resources;

namespace PMP.Documentation
{
    public partial class ScopeManagement_Review : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            FormUtil.Authenticate(Session, Response);

            if (!IsPostBack)
            {
                txtGoalID.Text = Request.Params["GOAL"];
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            // No item selected
            String selectedReviewers = txtReviewers.Text.Trim();
            if (selectedReviewers.Equals(String.Empty))
            {
                Messenger.Show(Response, "Please select at least one reviewer!");
                return;
            }

            // Send emails
            try
            {
                String mailSender = FormUtil.GetSessionUserName(Session);
                String ownerID = GoalsOperator.ReadGoalOwner(txtGoalID.Text.Trim());
                String owner = ResourceOperator.ReadResourceName(ownerID);
                String mailTos = selectedReviewers + ";" + owner;
                String subject = "Scope Needs Review: " + GoalsOperator.ReadGoalDesc(txtGoalID.Text.Trim());
                String body = "http://bs-pc-664:9090/Documentation/ScopeManagement.aspx?GOAL=" + txtGoalID.Text.Trim();

                DetailDesignOperator.SubmitToReview(mailTos, subject, mailSender, body);
            }
            catch (Exception ex)
            {
                Messenger.Show(Response, "Error: " + ex.Message);
                return;
            }

            Messenger.Show(Response, "Submit to Reivew Successfully!");
            Close();
        }

        protected void btnSelect_Click(object sender, EventArgs e)
        {
            String selectedReviewers = String.Empty;

            foreach (ListItem item in lbReviewers.Items)
            {
                if (item.Selected)
                {
                    if (selectedReviewers.Equals(String.Empty))
                    {
                        selectedReviewers += item.Text;
                    }
                    else
                    {
                        selectedReviewers += ";" + item.Text;
                    }
                }
            }

            txtReviewers.Text = selectedReviewers;
        }

        private void Close()
        {
            PopupForm submitToReview = new PopupForm(Response);
            submitToReview.Close();
        }
    }
}
